package it.polimi.TravelDream;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


/**
 * Session Bean implementation class BaseProductMgrBean
 */
@Stateless
public class BaseProductMgrBean implements BaseProductMgr {

	
	@PersistenceContext
    private EntityManager em;
	
	@Resource
	private EJBContext context;
	
  public User getCurrentUser() {
	return em.createNamedQuery(User.GetCurrent, User.class)
			.setParameter("param", context.getCallerPrincipal().getName())
            .getSingleResult();
}
	
    public void saveFlight(FlightDTO flight) {
    	Flight newFlight = new Flight(flight);
    	newFlight.setUser(getCurrentUser());
    	em.persist(newFlight);
    }
    
    public void saveHotel(HotelDTO hotel) {
    	Hotel newHotel = new Hotel(hotel);
    	newHotel.setUser(getCurrentUser());
    	em.persist(newHotel);
    }
    
    public void saveExcursion(ExcursionDTO excursion) {
    	Excursion newExcursion = new Excursion(excursion);
    	newExcursion.setUser(getCurrentUser());
    	em.persist(newExcursion);
    }
    
    public void updateFlight(FlightDTO flight) {
        // TODO Auto-generated method stub
    }

    public FlightDTO getFlightDTO() {
        // TODO Auto-generated method stub
			return null;
    }
    
    public void updateHotel(HotelDTO hotel) {
        // TODO Auto-generated method stub
    }

    public FlightDTO getHotelDTO() {
        // TODO Auto-generated method stub
			return null;
    }
    
    public void updateExcursion(ExcursionDTO excursion) {
        // TODO Auto-generated method stub
    }

    public FlightDTO getExcursiontDTO() {
        // TODO Auto-generated method stub
			return null;
    }
    
    protected static FlightDTO convertFlightToDTO(Flight flight){
    	FlightDTO flightDTO = new FlightDTO();
    	flightDTO.setId(flight.getId());
    	flightDTO.setArrAirport(flight.getArrAirport());
    	flightDTO.setArrLocation(flight.getArrLocation());
    	flightDTO.setCapacity(flight.getCapacity());
    	flightDTO.setDate_Time(flight.getDate_Time());
    	flightDTO.setDepAirport(flight.getDepAirport());
    	flightDTO.setDepLocation(flight.getDepLocation());
    	flightDTO.setFlag(flight.getFlag());
    	flightDTO.setPrice(flight.getPrice()); 
    	flightDTO.setDescription(flight.getDescription());
    	
    	return flightDTO;
    }
    
    protected static HotelDTO convertHotelToDTO(Hotel hotel){
    	HotelDTO hotelDTO = new HotelDTO();
    	hotelDTO.setId(hotel.getId());
    	hotelDTO.setAddress(hotel.getAddress());
    	hotelDTO.setCap(hotel.getCap());
    	hotelDTO.setCategory(hotel.getCategory());
    	hotelDTO.setCity(hotel.getCity());
    	hotelDTO.setCountry(hotel.getCountry());
    	hotelDTO.setDescription(hotel.getDescription());
    	hotelDTO.setName(hotel.getName());
    	hotelDTO.setPrice_Night(hotel.getPrice_Night());
    	hotelDTO.setWebsite(hotel.getWebsite());
    	
    	return hotelDTO;
    }

    protected static ExcursionDTO convertExcursionToDTO(Excursion excursion){
    	ExcursionDTO excursionDTO = new ExcursionDTO();
    	excursionDTO.setCapacity(excursion.getCapacity());
    	excursionDTO.setCity(excursion.getCity());
    	excursionDTO.setDate(excursion.getDate());
    	excursionDTO.setDescription(excursion.getDescription());
    	excursionDTO.setId(excursion.getId());
    	excursionDTO.setName(excursion.getName());
    	excursionDTO.setPrice(excursion.getPrice());
    	excursionDTO.setWebsite(excursion.getWebsite());
    	excursionDTO.setType(excursion.getType());
    	
    	return excursionDTO;
    }


}
